﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookStore.Admin.Models;
using System.Data;
namespace BookStore.Admin.Controllers
{
    [HandleError]
    public class CommentController : Controller
    {
        // view login
        public ActionResult index()
        {
            int page = 0 ;
            int limit = 5;
            string[][] AllComment ;
            CommentModel cm = new CommentModel();
            if (this.Session["Session_Admin"] == null)
            {
                Response.Redirect("/admin/login");
            }
            if ((this.Request.QueryString["page"] != null))
            {
                try
                {
                    page = Convert.ToInt32(this.Request.QueryString["page"]);
                }
                catch (SystemException se)
                {
                    page = 1;
                }
            }
            else
            {
                page = 1;
            }
            string url = "/admin/comment";
            string []keyword  = new string [3];
            string keyword_temp = "";
            int total_comment = 0;
            if((this.Request.Form["keyword"] != null) && (this.Request.Form["keyword"] != ""))
            {
                keyword = this.Request.Form["keyword"].Split(Convert.ToChar("-"));
                keyword_temp = this.Request.Form["keyword"].ToString();
            }
            else if((this.Request.QueryString["keyword"] != null) && (this.Request.QueryString["keyword"] != ""))
            {
                keyword = this.Request.QueryString["keyword"].Split(Convert.ToChar("-"));
                keyword_temp = this.Request.QueryString["keyword"].ToString();
            }
            if ((keyword.Length == 3)&&(keyword[0]!=null)&&(keyword[0]!=""))
            {
                AllComment = cm.getCommentByKeyword(page, limit, keyword_temp);
                total_comment = cm.countAllCommentByKeyword(keyword_temp);
                url = url + "?keyword=" + keyword_temp + "&page=";
            }
            else
            {
                AllComment = cm.getAllComment(page, limit);
                total_comment = cm.countAllComment();
                url = url + "?page=";
            }
            ViewData["AllComment"] = AllComment;
            Pagination pagination = new Pagination(url, total_comment, limit, page);
            ViewData["pagination"] = pagination.render();
            try
            {
                int success = Convert.ToInt32(this.Session["success"]);
                if (success == 1 || success == 2)
                {
                    ViewData["success"] = success;
                    this.Session["success"] = null;
                }
            }
            catch(SystemException se)
            {
                 Response.Redirect("/home/index");
            }

            return View();
        }
        public ActionResult delete()
        {
            CommentModel cm = new CommentModel();
            int id = 0;
            int err = 0;
            try
            {
               id = Convert.ToInt32(this.Request.QueryString["cid"].ToString());
            }
            catch (SystemException se)
            {
                this.Session["success"] = 2;
                Response.Redirect("/admin/comment");
            }
            err = cm.deleteCommentById(id);
            if (err == -1)
            {
                this.Session["success"] = 2;
                Response.Redirect("/admin/comment");
            }
            this.Session["success"] = 1;
            Response.Redirect("/admin/comment");
            return View();
        }
        public ActionResult deletemany()
        {
            CommentModel cm = new CommentModel();
            int id = 0;
            if (this.Request.Form["selected[]"] != null)
            {

                foreach (string value in this.Request.Form["selected[]"].Split(','))
                {
                    try
                    {
                        id = Convert.ToInt32(value.ToString());
                    }
                    catch (SystemException se)
                    {
                        this.Session["success"] = 2;
                        Response.Redirect("/admin/comment");
                    }
                    cm.deleteCommentById(id);
                }
                this.Session["success"] = 1;
                Response.Redirect("/admin/comment");
            }
            else
            {
                Response.Redirect("/admin/comment");

            }
            return View();
        }

       
       
    }
}
